package com.zhongzhi.javafx.controller.updata;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

import com.zhongzhi.common.Common;
import com.zhongzhi.common.CommonUpdata;
import com.zhongzhi.springboot.pojo.Computers;
import com.zhongzhi.springboot.pojo.Equipment;
import com.zhongzhi.springboot.pojo.EquipmentShow;
import com.zhongzhi.util.ClientUtil;
import com.zhongzhi.util.JsonResult;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.MenuButton;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import lombok.extern.log4j.Log4j;
import javafx.scene.control.TextArea;
@Log4j
public class EquipmentUpdataViewController implements Initializable{
	@FXML
    private TextField plcName;
    @FXML
    private TextField host;
    @FXML
    private TextField port;
    @FXML
    private MenuButton upper;
    @FXML
    private TextArea remarks;
    @FXML
    private Button stop;
    @FXML
    private Button add;
    @FXML
    private Label textts;
    
    private Integer computersid = null;
    
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		log.info("修改设备窗口:开始初始化");
		
		Select();
		//取消按钮
		stop.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				log.info("修改设备窗口:按下取消按钮");
				Stage window = (Stage) stop.getScene().getWindow();
				window.close();
			}
		});
		//确定按钮
		add.setOnAction(new EventHandler<ActionEvent>() {
			
			@Override
			public void handle(ActionEvent event) {
				log.info("修改设备窗口:按下确定按钮");
				if (plcName.getText()==null || plcName.getText().equals("")) {
					textts.setText("设备名称不能为空");
				}else if (host.getText()==null || host.getText().equals("")) {
					textts.setText("设备ip地址不能为空");
				}else if (host.getText().matches("([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}")==false) {
					textts.setText("设备ip地址格式不正确");
				}else if (port.getText()==null || port.getText().equals("")) {
					textts.setText("设备端口不能为空");
				}else if (port.getText().matches("-?[1-9]\\d*")==false) {
					textts.setText("设备端口必须为整数");
				}else if (computersid==null) {
					textts.setText("上位机站号不能为空");
				}else {
					if (remarks.getText()==null || remarks.getText().equals("")) {
						remarks.setText("无");
					}
					int a=0;
					try {
						a=Integer.valueOf(port.getText());
						if (a>65535||a<0) {
							textts.setText("端口超范围,0-65535");
						}else {
							textts.setText("");
							insert();
						}
					} catch (Exception e) {
						a=2000;
						textts.setText("端口必须为纯数字");
					}
					
				}
			}
		});
		EquipmentShow equipment = CommonUpdata.getEquipment();
		plcName.setText(equipment.getPlcName());
		host.setText(equipment.getHost());
		port.setText(equipment.getPort().toString());
		remarks.setText(equipment.getRemarks());
		computersid = equipment.getId();
		if (equipment.getRemarks()==null || equipment.getRemarks().equals("")) {
			remarks.setText("无");
		}else {
			remarks.setText(equipment.getRemarks());
		}
		
		log.info("修改设备窗口:初始化完成");
	}
	
	private void Select() {
		try {
			String s = "http://127.0.0.1:8080/computers/select?pages=1&line=255";
			log.info("修改工位窗口:设备查询:"+s);
			String gethttp = ClientUtil.Gethttp(s);
			log.info("修改工位窗口:设备查询结果:"+gethttp);
			JsonResult jsonobj = ClientUtil.Jsonobj(gethttp, JsonResult.class);
			if (jsonobj!=null) {
				String string = jsonobj.getData().toString();
				List<Computers> eqlist = ClientUtil.Jsonlist(string,Computers.class);
				for (Computers computers : eqlist) {
					MenuItem menuItem = new MenuItem(computers.getName());
					//增加事件
					menuItem.setOnAction(new EventHandler<ActionEvent>() {
						
						@Override
						public void handle(ActionEvent event) {
							upper.setText(computers.getName());
							computersid = computers.getId();
						}
					});
					
					upper.getItems().add(menuItem);
					
					EquipmentShow equipment = CommonUpdata.getEquipment();
					if (equipment.getComputersId()==computers.getId()) {
						upper.setText(computers.getName());
					}
				}
				
			}
		} catch (Exception e) {
			log.error("修改工位窗口:设备查询失败"+e);
		}
		
	}
	//插入数据库
		private void insert() {
			
			try {
				EquipmentShow equipment = CommonUpdata.getEquipment();
				String s = "http://127.0.0.1:8080/equipment/update?id="+equipment.getId()+"&plcName=" + plcName.getText().trim()
						+ "&host=" + host.getText().trim()
						+ "&port=" + port.getText().trim()
						+ "&upper=" + computersid
						+ "&remarks=" + remarks.getText().trim();
						
						log.info("修改设备窗口:发送修改设备请求:"+s);
						String gethttp = ClientUtil.Gethttp(s);
						log.info("修改设备窗口:修改设备请求返回数据:"+gethttp);
						JsonResult jsonobj = ClientUtil.Jsonobj(gethttp, JsonResult.class);
						if (jsonobj.getCode()==200) {
							ScrollPane sp = Common.getSp();
							sp.setContent(FXMLLoader.load(getClass().getResource("/fxml/run/Equipment.fxml")));
							Stage window = (Stage) stop.getScene().getWindow();
							window.close();
						}else {
							textts.setText(jsonobj.getMessage());
						}
			} catch (Exception e) {
				log.error("修改设备窗口:修改设备请求返回数据失败:"+e);
			}
			
		}

}
